<template>
  <div class="top-category">
    <div class="container">
      <!-- 渲染面包屑导航 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem :key="category.id" v-if="category">
            {{ category?.name }}
          </XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 因为分类数据是发请求拿到的，计算属性要通过分类数据拿到，请求如果还没回来，拿不到分类数据会报错，所以需要用?.的方式 -->
      <XtxCarousel
        :slides="slides"
        style="height: 500px"
        autoPlay
      ></XtxCarousel>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in category?.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in subList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem
            v-for="goods in item.goods"
            :key="goods.id"
            :goods="goods"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { findBanner } from '@/api/home'
import GoodsItem from './components/goods-item'
import { findTopCategory } from '@/api/category'
export default {
  name: 'TopCategory',
  components: {
    GoodsItem
  },
  setup() {
    const route = useRoute()
    const store = useStore()
    // 轮播图数据
    const slides = ref([])
    // 获取分类数据
    const subList = ref([])
    // 轮播图
    findBanner().then(res => {
      slides.value = res.result
    })
    // 单个顶级分类信息
    findTopCategory(route.params.id).then(res => {
      subList.value = res.result.children
    })
    // 监听路由id的变化
    watch(
      () => route.params.id,
      value => {
        // 切换到二级类目路由的时候也有ID，但是也触发了watch导致发送了请求，需要处理。 包含/sub就是二级，就不应该发送请求
        // 如果切换到了2级分类，那么就不应该发送请求了
        if (route.path.includes('/sub')) return
        findTopCategory(value).then(data => {
          subList.value = data.result.children
        })
      },
      // 上来就先监听一次
      {
        immediate: true
      }
    )
    // 计算属性通过地址栏id和vuex中的数据匹配到对应的
    const category = computed(() => {
      return store.state.category.list.find(item => item.id === route.params.id)
    })
    return {
      category,
      slides,
      subList
    }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
